#include "entity.h"
#include "game.h"
#include <QDebug>


Entity::Entity(const quint32 ID)
   : mID(ID),
     mActive(true),
     mUpdated(false),
     mRotation(0.0),
     mHealth(100),
     mTarget(0),
     mSquadID(0)
{
   mBaseStats.type = ENTITY_NONE;
   mBaseStats.kind = 0;
   mBaseStats.size = 0;
   mBaseStats.color = 0;
}


Entity::~Entity()
{
   //qDebug() << "~Entity id=" << id();
}


void Entity::marshall(const EVENT_TYPE event, QDataStream &data)
{
   switch (event)
   {
   case EVENT_ENTITY_ADD:
      data << (qint32)mBaseStats.type
           << (quint32)mID
           << (quint32)mPlayerID
           << (quint32)mSquadID
           << (qint32)mBaseStats.kind
           << (qint32)mBaseStats.size
           << mBaseStats.color
           << mPosition.x()
           << mPosition.y()
           << mRotation;
      break;
   default:
      break;
   }

}


void Entity::demarshall(const EVENT_TYPE event, QDataStream &data, const quint32 playerID)
{
   qreal posX=0.0, posY=0.0;
   quint32 squadSize;

   switch (event)
   {
   case EVENT_ENTITY_SQUAD_ADD:
      data >> mSquadID
           >> squadSize
           >> mBaseStats.kind
           >> mBaseStats.size
           >> mBaseStats.color
           >> posX
           >> posY;
      break;
   case EVENT_ENTITY_ADD:
      data >> mBaseStats.kind
           >> mBaseStats.size
           >> mBaseStats.color
           >> posX
           >> posY;
      setPlayerID(playerID);
      break;
   default:
      break;
   }

   setPosition(QPointF(posX, posY));
   qDebug() << "Entity::demarshall type=" << mBaseStats.type << "kind=" << mBaseStats.kind << "pos=" << position();
}


// Setters/Getters
ENTITY_TYPE Entity::type() { return mBaseStats.type; }

quint32 Entity::id() { return mID; }
void Entity::setID(const quint32 id) { mID = id; }

void Entity::setPlayerID(const quint32 playerID) { mPlayerID = playerID; }
quint32 Entity::playerID() { return mPlayerID; }

bool Entity::active() { return mActive; }
void Entity::setActive(const bool active) { mActive = active; }

bool Entity::updated() { return mUpdated; }
void Entity::setUpdated(bool updated) { mUpdated = updated; }

QVector2D Entity::position() { return mPosition; }
void Entity::setPosition(const QPointF &pos) { mPosition = QVector2D(pos); }

QVector2D Entity::velocity() { return mVelocity; }
void Entity::setVelocity(const QPointF &vel) { mVelocity = QVector2D(vel); }

QVector2D Entity::acceleration() { return mAcceleration; };
void Entity::setAcceleration(const QPointF &acl) { mVelocity = QVector2D(acl); }

qreal Entity::rotation() { return mRotation; };
void Entity::setRotation(const qreal rotation) { mRotation = rotation; }

EntityBaseStats Entity::baseStats() { return mBaseStats; }
void Entity::setBaseStats(const EntityBaseStats &stats) { mBaseStats = stats; }

qint32 Entity::kind() { return mBaseStats.kind; }
void Entity::setKind(const qint32 kind) { mBaseStats.kind = kind; }

qint32 Entity::size() { return mBaseStats.size; }
void Entity::setSize(const qint32 size) { mBaseStats.size = size; }

QRgb Entity::color() { return mBaseStats.color; }
void Entity::setColor(const QRgb color) { mBaseStats.color = color; };

qint32 Entity::health() { return mHealth; }
void Entity::setHealth(const qint32 health) { mHealth = health; }

Entity* Entity::target() { return mTarget; }
void Entity::setTarget(Entity *target) { mTarget = target; }

quint32 Entity::squadID() { return mSquadID; }
void Entity::setSquadID(const quint32 id) { mSquadID = id; }

QTime Entity::nextThinkTime() { return mNextThinkTime; }
void Entity::setNextThinkTime(const QTime &time) { mNextThinkTime = time; }

void Entity::setGame(Game *game) { mGame = game; setNextThinkTime(mGame->currentTime()); }

void Entity::onAddToGame() { }
